package com.zulu.karaoke.gui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.zulu.karaoke.R;
import com.zulu.karaoke.actionbar.ActionModeHelper;
import com.zulu.karaoke.adapter.ListFavouriteAdapter;
import com.zulu.karaoke.database.DatabaseManager;
import com.zulu.karaoke.database.SongTableHelper;
import com.zulu.karaoke.gui.custom.CustomAlertDialog;
import com.zulu.karaoke.model.SongModel;
import com.zulu.karaoke.utils.GeneralUtils;

public class FragmentFullListFavourite extends Fragment {

	private Context mContext;
	private SongTableHelper insSongHelper;
	private List<String> lstNumber;
	private List<String> lstTitle;
	private ListFavouriteAdapter adapter;
	private final String TAG = "FavouriteFragment";
	private Button mBtnRefresh;
	private Button mBtnClear;
	private ListView mLstViewFavourite;
	private static FragmentFullListFavourite instance;

	public static FragmentFullListFavourite getInstance() {
		if (instance == null) {
			instance = new FragmentFullListFavourite();
		}
		return instance;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// return super.onCreateView(inflater, container, savedInstanceState);
		mContext = this.getActivity();
		insSongHelper = DatabaseManager.getInstance().getSongTableHelper();
		final View view = inflater.inflate(
				R.layout.fragment_full_favourite_list, null);

		mBtnRefresh = (Button) view
				.findViewById(R.id.btn_refresh_favourite_full);
		mBtnClear = (Button) view.findViewById(R.id.btn_clear_favourite_full);
		mLstViewFavourite = (ListView) view
				.findViewById(R.id.lst_view_favourite);

		refreshListView();

		mBtnRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				refreshListView();
			}
		});

		mBtnClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mLstViewFavourite.getCount() == 0) {
					GeneralUtils.showToastMessage(getActivity(), getActivity()
							.getString(R.string.mess_no_favourite_to_clear));
				} else {
					final View childView = LayoutInflater.from(getActivity())
							.inflate(R.layout.custom_dialog_simple_layout,
									null, false);
					TextView textView = (TextView) childView
							.findViewById(R.id.tv_mess_content);
					textView.setText(getActivity().getString(
							R.string.mess_clear_favourite_confirm));
					final CustomAlertDialog alert = new CustomAlertDialog(
							getActivity(), childView, true);
					alert.setTitle(getActivity().getString(
							R.string.title_clear_confirm));
					alert.setButton(
							getActivity().getString(R.string.button_text_ok),
							new OnClickListener() {

								@Override
								public void onClick(View v) {
									List<SongModel> lstFavouriteSong = insSongHelper
											.getAllFavouriteSong();
									for (SongModel song : lstFavouriteSong) {
										song.setSongFavourite("0");
										insSongHelper.updateSong(song);
									}
									refreshListView();
									alert.hideDialog();
								}
							});
					alert.setButton2(
							getActivity()
									.getString(R.string.button_text_cancel),
							new OnClickListener() {

								@Override
								public void onClick(View v) {
									alert.hideDialog();
								}
							});
					alert.showDialog();
				}
			}
		});

		mLstViewFavourite.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> l, View v, int position, long id) {
				showSongDetailFavourite(position);
//				GeneralUtils.showPopupFavoriteSongDetail(mContext, insSongHelper, position, 
//						adapter, FragmentFullListFavourite.this, getListFavouriteNumber(), 
//						getListFavouriteTitle(), mLstViewFavourite, lstTitle, lstNumber);
			}
		});
		
		mLstViewFavourite.setOnItemLongClickListener(new ActionModeHelper(this, mLstViewFavourite, lstTitle, lstNumber));
		
		return view;
	}
	
	private void showSongDetailFavourite(int position) {

		// TODO Auto-generated method stub
		final SongModel song = insSongHelper
				.getSongObjectBaseNumber(lstNumber.get(position));
		final View childView = LayoutInflater.from(getActivity())
				.inflate(R.layout.song_details_dialog, null, false);

		TextView tvSongNumber = (TextView) childView
				.findViewById(R.id.tv_song_number);
		TextView tvSongTitle = (TextView) childView
				.findViewById(R.id.tv_song_title);
		TextView tvSongBegin = (TextView) childView
				.findViewById(R.id.tv_song_begin);
		TextView tvSongComposer = (TextView) childView
				.findViewById(R.id.tv_song_composer);
		TextView tvSongAlbum = (TextView) childView
				.findViewById(R.id.tv_song_album);
		tvSongNumber.setText(song.getSongNumber());
		tvSongTitle.setText(song.getSongTitle());
		tvSongBegin.setText(song.getSongBegin());
		tvSongComposer.setText(song.getSongComposer());
		tvSongAlbum.setText(song.getSongCategory());
		Log.i(TAG, song.getSongNumber());

		final CustomAlertDialog alert = new CustomAlertDialog(
				getActivity(), childView, true);

		alert.setTitle(getActivity()
				.getString(R.string.title_song_info));

		alert.setButton(
				getActivity().getString(
						R.string.button_text_remove_favourite),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						song.setSongFavourite("0");
						insSongHelper.updateSong(song);
						GeneralUtils
								.showToastRemoveFavouriteDone(mContext);
						alert.hideDialog();
						refreshListView();
					}
				});

		alert.setButton2(
				getActivity().getString(R.string.button_text_cancel),
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						alert.hideDialog();
					}
				});
		alert.showDialog();
	
	}

	/**
	 * Implement action bar in favorite fragment
	 * @param item menu in action bar menu
	 * @param position in menu item
	 * @return boolean value
	 */
	public boolean performAction(MenuItem item, int position) {

		switch (item.getItemId()) {
		case R.id.view:
			showSongDetailFavourite(position);
			return (true);
		case R.id.remove:
			final SongModel song = insSongHelper.getSongObjectBaseNumber(lstNumber.get(position));
			song.setSongFavourite("0");
			insSongHelper.updateSong(song);
			GeneralUtils.showToastRemoveFavouriteDone(mContext);
			refreshListView();
			return (true);
		}

		return (false);
	}
	
//	@Override
//	public void onCreateContextMenu(ContextMenu menu, View v,
//			ContextMenu.ContextMenuInfo menuInfo) {
//		new MenuInflater(getActivity()).inflate(R.menu.favourite_action, menu);
//	}

	private void refreshListView() {
		adapter = new ListFavouriteAdapter(mContext, getListFavouriteNumber(),
				getListFavouriteTitle());
		adapter.setNotifyOnChange(true);
		mLstViewFavourite.setAdapter(adapter);
		mLstViewFavourite.setOnItemLongClickListener(new ActionModeHelper(this, mLstViewFavourite, lstTitle, lstNumber));
	}

	private String[] getListFavouriteTitle() {
		lstTitle = new ArrayList<String>();
		lstTitle = insSongHelper.getAllFavouriteTitle();
		String[] result = new String[lstTitle.size()];
		for (int i = 0; i < lstTitle.size(); i++) {
			result[i] = lstTitle.get(i);
		}
		return result;
	}

	private String[] getListFavouriteNumber() {
		lstNumber = new ArrayList<String>();
		lstNumber = insSongHelper.getAllFavouriteNumber();
		String[] result = new String[lstNumber.size()];
		for (int i = 0; i < lstNumber.size(); i++) {
			result[i] = lstNumber.get(i);
		}
		return result;
	}

}
